# -*- coding: utf-8 -*-
#
# pyOSCAR
#
# pyOSCAR is a python implementation of the official OSCAR protocol. pyOSCAR speaks to the
# AIM backend using the same protocol that official native AOL clients use.
#
# See OSCAR protocol specification at http://dev.aol.com/aim/oscar/
# Copyright (C) 2007 S.H.Mohanjith, http://www.mohanjith.net
#
# pyOSCAR is free software; you can redistribute it and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# pyOSCAR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program;
# if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301, USA
#

from pyoscar import pyOscar
from hashlib import sha256

import urllib2, urllib
import xml.sax, xml.sax.handler
import hmac, base64

class AuthHandler(xml.sax.handler.ContentHandler):
    def __init__(self):
        self.mapping = {}
        self.buffer = ""

    def startElement(self, name, attributes):
        if name == "token":
            self.mapping['token'] = {}

    def characters(self, data):
        self.buffer = data

    def endElement(self, name):
        if name == "statusCode":
            self.mapping['statusCode'] = self.buffer
        elif name == "statusText":
            self.mapping['statusText'] = self.buffer
        elif name == "expiresIn":
            self.mapping['token']['expiresIn'] = self.buffer
        elif name == "a":
            self.mapping['token']['a'] = self.buffer
        elif name == "sessionSecret":
            self.mapping['sessionSecret'] = self.buffer
        elif name == "hostTime":
            self.mapping['hostTime'] = self.buffer


class Auth(pyOscar):
    username = None
    password = None
    session_secret = None
    session_key = None
    expires_in = None
    format = 'xml'

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def login(self):
        url = 'https://api.screenname.aol.com/auth/clientLogin?f='+self.format
        data = urllib.urlencode([('k', self.open_aim_key), ('s', self.username),
                                 ('pwd', self.password), ('clientVersion', self.client_version),
                                 ('clientName', self.client_name)])
        req = urllib2.Request(url)
        rdata = urllib2.urlopen(req, data)

        parser = xml.sax.make_parser()
        handler = AuthHandler()

        parser.setContentHandler(handler)
        parser.parse(rdata)

        self.session_secret = handler.mapping['sessionSecret']
        self.session_key = base64.b64encode(hmac.new(self.session_secret,self.password, sha256).digest())
        self.expires_in = handler.mapping['token']['expiresIn']
